﻿using SiliconSelect.Base;
using SiliconSelect.BLL;
using SiliconSelect.Models;
using SiliconSelect.PlcComunication;
using SiliconSelect.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SiliconSelect.Views.UserOperatorDialog
{
    /// <summary>
    /// CrystalFinishDialog.xaml 的交互逻辑
    /// </summary>
    public partial class CrystalFinishDialog : Window
    {
        CrystalFinishViewModel viewModel = new CrystalFinishViewModel();
        public CrystalFinishDialog(int theoryCount, int realCount)
        {
            InitializeComponent();
            DataContext = viewModel;
            viewModel.TheoryCountChange = theoryCount;
            viewModel.RealCount = realCount;
            viewModel.WarnTextShow = $"实际片数已经达到理论片数最大值,理论片数:{theoryCount},实际片数:{realCount}.请根据具体生产情况进行后续操作!";
        }

        /// <summary>
        /// 点击了接单按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FinishCrystalBtnClick(object sender, RoutedEventArgs e)
        {
            GlobalValues.CrystalFinishState = true;
            GlobalValues.DialogIsVisible = false;
            Close(); // 直接关闭即可. 关闭之后,如何防止再弹窗呢
        }

        /// <summary>
        /// 继续生产的逻辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ContinueWorkingClick(object sender, RoutedEventArgs e)
        {
            // 继续生产,这个时候,要去更新下WorkingInfo中的理论片数,但是理论片数要比当前的实际片数大才行.
            if (viewModel.TheoryCountChange <= viewModel.RealCount)
            {
                MessageBox.Show("理论片数必须比实际片数大,才可以继续作业,如果已经没有硅片,请接单!", "操作提示", MessageBoxButton.OK, MessageBoxImage.Hand);
            }
            else
            {
                // 继续自动作业,先保存更新新的实际片数
                LocalDataAccess.UpdateWorkingInfo(new List<string> { "TheoryCounts" }, new List<string> { viewModel.TheoryCountChange.ToString() });
                PlcCommunication.Instance.PlcLoadControl("Open");
                // 然后关闭当前的界面
                GlobalValues.DialogIsVisible = false;
                Close();
            }
        }
    }
}
